#include <bits/stdc++.h>
using namespace std;

bool is_Prime(const unsigned int n){
    for (int i=2;i<=sqrt(n); i++)
        if (n%i==0)
            return false;
    return (n>1);
}

int main(){
    int n,m,tgx,tgy,count=0;
    cin>>n>>m;
    int arr[n+5][m+5];
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            cin>>arr[i][j];
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j)
            if (is_Prime(arr[i][j])){
                arr[i][j]=-1;
                count=1;
                tgx=i;
                tgy=j;
                //cout<<tgx<<' '<<tgy<<endl;
                break;
            }
        if (count)
            break;
    }
    for (int j = 1; j <= m; ++j)
        if (is_Prime(arr[tgx][j])) {
            arr[tgx][j]=-1;
            ++count;
            //cout<<tgx<<' '<<j<<endl;
        }
    for (int i = 1; i <= n; ++i)
        if (is_Prime(arr[i][tgy])) {
            arr[i][tgy]=-1;
            ++count;
            //cout<<i<<' '<<tgy<<endl;
        }
    for (int i = 1,j; i <= n; ++i) {
        j = i - tgx + tgy;
        if (j <= 0 or j > m)
            continue;
        if (is_Prime(arr[i][j])) {
            arr[i][j]=-1;
            ++count;
            //cout<<i<<' '<<j<<endl;
        }
    }
    for (int j = 1,i; j <= m; ++j) {
        i=tgx+tgy-j;
        if (i<=0 or i>n)
            continue;
        if (is_Prime(arr[i][j])) {
            arr[i][j]=-1;
            ++count;
            //cout<<i<<' '<<j<<endl;
        }
    }
    cout<<count<<endl;
    return 0;
}